package com.yimi.yinhepda.db.dao;

import android.database.Cursor;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.support.DatabaseConnection;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.helper.DbHelperUC;

import java.sql.SQLException;
import java.util.List;

/**
 * @author lj
 */
public class BaseDaoUC<T, ID> implements IBaseDao<T, ID> {

    protected Dao<T, ID> dao;
    private DbHelperUC dbHelper;

    public BaseDaoUC(OrmLiteSqliteOpenHelper ormLiteSqliteOpenHelper, Class<T> tClass) {
        this.dbHelper = (DbHelperUC) ormLiteSqliteOpenHelper;
        if (dbHelper == null) {
            return;
        }
        try {
            dao = dbHelper.getDao(tClass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库帮助类
     *
     * @return 数据库帮助类 db helper
     */
    public DbHelperUC getDbHelper() {
        return dbHelper;
    }

    /**
     * 指定Id查询
     */
    @Override
    public <T> T findById(ID id) throws SQLException {
        DatabaseConnection databaseConnection = null;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            T t = (T) dao.queryForId(id);
            dao.commit(databaseConnection);
            return t;
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return null;
    }

    /**
     * 查询所有
     */
    @Override
    public List<T> findAll() {
        List<T> arrayList = null;
        try {
            arrayList = dao.queryForAll();
            return arrayList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Cursor findFirstData(String tableName) throws SQLException {
        return null;
    }

    /**
     * 删除 返回值为影响行数
     */
    @Override
    public int delete(T entity) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.delete(entity);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return i;
    }

    /**
     * 指定id删除 返回值正常为1
     */
    @Override
    public int deleteById(ID id) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.deleteById(id);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return i;
    }

    @Override
    public int delete(List<T> entities) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.delete(entities);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            if (dao != null && databaseConnection != null) {
                dao.endThreadConnection(databaseConnection);
            }
        }
        return i;
    }

    @Override
    public int deleteAll() {
        int i = -1;
        try {
            DeleteBuilder deleteBuilder = dao.deleteBuilder();
            i = deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 插入一行记录
     */
    @Override
    public int insert(T entity) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.create(entity);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return i;
    }

    @Override
    public int insert(final List<T> entities) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.create(entities);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return i;
    }

    @Override
    public int update(T entity) throws SQLException {
        DatabaseConnection databaseConnection = null;
        int i = -1;
        try {
            databaseConnection = dao.startThreadConnection();
            dao.setAutoCommit(databaseConnection, false);
            i = dao.update(entity);
            dao.commit(databaseConnection);
        } catch (SQLException e) {
            if (dao != null && databaseConnection != null) {
                dao.rollBack(databaseConnection);
            }
            e.printStackTrace();
        } finally {
            dao.endThreadConnection(databaseConnection);
        }
        return i;
    }
}
